package com.chainshop.restock.controller;

import com.chainshop.common.annotation.Log;
import com.chainshop.common.core.controller.BaseController;
import com.chainshop.common.core.domain.AjaxResult;
import com.chainshop.common.core.domain.pojo.RestockProduct;
import com.chainshop.common.core.domain.pojo.Stock;
import com.chainshop.common.core.page.TableDataInfo;
import com.chainshop.common.enums.BusinessType;
import com.chainshop.common.utils.poi.ExcelUtil;
import com.chainshop.restock.service.IRestockStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 用户信息
 * @author hwj
 */
@RestController
@RequestMapping("/restock/stock")
public class RestockStoreController extends BaseController {
    @Autowired
    private IRestockStoreService restockService;

    /**
     * 获取商品库存列表
     */
    @PreAuthorize("@ss.hasPermi('restock:stock:list')")
    @GetMapping("/list")
    public TableDataInfo list(Stock stock) {
        startPage();
        System.out.println(stock);
        List<Stock> list = restockService.selectStockList(stock);
        return getDataTable(list);
    }

    /**
     * 根据所需商品，获取商品库存列表
     */
    @PreAuthorize("@ss.hasPermi('restock:stock:list')")
    @GetMapping("/stockList")
    public TableDataInfo getListStock(RestockProduct restockProduct) {
        startPage();
        List<Stock> stocks = restockService.selectStockListByProductId(restockProduct);
        return getDataTable(stocks);
    }

    @PreAuthorize("@ss.hasPermi('restock:stock:edit')")
    @GetMapping("/{stockId}")
    public AjaxResult getStock(@PathVariable(value = "stockId") Integer stockId) {
        return AjaxResult.success(restockService.selectStockById(stockId));
    }

    @PreAuthorize("@ss.hasPermi('restock:stock:edit')")
    @Log(title = "存储库存信息", businessType = BusinessType.UPDATE)
    @PutMapping("/stock")
    public AjaxResult updateStock(@RequestBody Stock stock) {
        return toAjax(restockService.updateStock(stock));
    }

    /**
     * 删除存储库存信息
     */
    @PreAuthorize("@ss.hasPermi('restock:stock:remove')")
    @Log(title = "存储库存信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(restockService.deleteStockByIds(ids));
    }


    @PreAuthorize("@ss.hasPermi('restock:stock:lsit')")
    @GetMapping("/store")
    public AjaxResult getStoreNames() {
        return AjaxResult.success(restockService.getStores());
    }

    @PreAuthorize("@ss.hasPermi('restock:stock:lsit')")
    @GetMapping("/storeInfo")
    public AjaxResult getStoreInfo() {
        return AjaxResult.success(restockService.getStoreByUserId());
    }


    /**
     * 导出存储库存信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:stock:export')")
    @Log(title = "存储库存信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Stock stock)
    {
        List<Stock> list = restockService.selectStockList(stock);
        ExcelUtil<Stock> util = new ExcelUtil<Stock>(Stock.class);
        util.exportExcel(response, list, "存储库存信息数据");
    }

}
